package com.ruoyi.web.wx.controller.pay;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.CustomId;
import com.ruoyi.system.domain.wx.WxIcon;
import com.ruoyi.system.domain.wx.WxUser;
import com.ruoyi.system.domain.wx.pay.WxPayType;
import com.ruoyi.system.service.wx.IWxIconService;
import com.ruoyi.system.service.wx.IWxUserService;
import com.ruoyi.system.service.wx.pay.IWxPayTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 支出类型Controller
 *
 * @author mic
 * @date 2023-08-17
 */
@RestController
@RequestMapping("/payType")
public class WxPayTypeController extends BaseController {
    @Autowired
    private IWxPayTypeService wxPayTypeService;
    @Autowired
    private IWxUserService userService;
    @Autowired
    private IWxIconService iconService;

    /**
     * 查询支出类型列表
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:list')")
    @GetMapping("/list")
    public AjaxResult list(WxPayType wxPayType) {
        List<WxPayType> list = wxPayTypeService.selectWxPayTypeList(wxPayType);
        for (int i = 0; i < list.size(); i++) {
            WxUser user = userService.selectWxUserByUserId(list.get(i).getCreateBy());
            list.get(i).setNickName(user.getNickName());
            WxIcon wxIcon = iconService.selectwxIconByIconId(list.get(i).getIcon());
            if (wxIcon != null) {
                list.get(i).setIconUrl(wxIcon.getUrl());
            }
        }
        return success(list);
    }

    /**
     * 导出支出类型列表
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:export')")
    @Log(title = "支出类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxPayType wxPayType) {
        List<WxPayType> list = wxPayTypeService.selectWxPayTypeList(wxPayType);
        ExcelUtil<WxPayType> util = new ExcelUtil<WxPayType>(WxPayType.class);
        util.exportExcel(response, list, "支出类型数据");
    }

    /**
     * 获取支出类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") String typeId) {
        return success(wxPayTypeService.selectWxPayTypeByTypeId(typeId));
    }

    /**
     * 新增支出类型
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:add')")
    @Log(title = "支出类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxPayType wxPayType) {
        wxPayType.setTypeId("pt" + CustomId.timestamp());
        wxPayType.setCreateBy(SecurityUtils.getUsername());
        wxPayType.setCreateDate(new Date());
        return toAjax(wxPayTypeService.insertWxPayType(wxPayType));
    }

    /**
     * 修改支出类型
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:edit')")
    @Log(title = "支出类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxPayType wxPayType) {
        return toAjax(wxPayTypeService.updateWxPayType(wxPayType));
    }

    /**
     * 删除支出类型
     */
    @PreAuthorize("@ss.hasPermi('payType:payType:remove')")
    @Log(title = "支出类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable String[] typeIds) {
        return toAjax(wxPayTypeService.deleteWxPayTypeByTypeIds(typeIds));
    }
}
